package com.caigou.controller;

import com.caigou.annotation.OperLog;
import com.caigou.bean.cms.entity.*;
import com.caigou.bean.cms.entity.AudioFolder;
import com.caigou.bean.cms.param.*;
import com.caigou.bean.exam.entity.Users;
import com.caigou.constant.NewsConstant;
import com.caigou.enums.ResponseEnum;
import com.caigou.security.utils.CurrentUser;
import com.caigou.service.AudioService;
import com.caigou.util.Result;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/audio/")
public class AudioController {
    @Resource
    private AudioService audioService;

    /**
     * 获取音频根文件夹
     * @param user
     * @return
     */
    @RequestMapping("getRootAudioFolder")
    @PreAuthorize("hasAnyAuthority('yinpin_quankuchaxun')")
    public Result<AudioFolder> getRootAudioFolder(@CurrentUser Users user){
        AudioFolder audioFolder = audioService.getRootAudioFolder();
        return Result.success(audioFolder);
    }

    /**
     * 创建音频文件夹
     * @param audioFolderParam
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.AUDIO_MODULE, operType = NewsConstant.ADD_OPERATION, operDesc = "创建音频库文件夹")
    @RequestMapping("createAudioFolder")
    @PreAuthorize("hasAnyAuthority('yinpin_muluguanli')")
    public Result createAudioFolder(@RequestBody AudioFolderParam audioFolderParam, @CurrentUser Users user){
        if(audioFolderParam.getSuperId()==null || audioFolderParam.getFolderName()==null){
            return Result.error(ResponseEnum.PARAM_ERROR);
        }
        if(audioFolderParam.getFolderName().length()>50){
            return Result.error("音频目录名称不能超过50个字", "音频目录名称不能超过50个字");
        }
        return audioService.createAudioFolder(audioFolderParam.getSuperId(), audioFolderParam.getFolderName(), user.getUserid());
    }

    @RequestMapping("deleteAudioFolder")
    @PreAuthorize("hasAnyAuthority('yinpin_muluguanli')")
    public Result deleteAudioFolder(@RequestBody AudioFolderParam audioFolderParam, @CurrentUser Users user){
        return audioService.deleteAudioFolder(audioFolderParam.getId());
    }

    /**
     * 列表某一音频文件夹下的所有子文件夹列表
     * @param audioFolderParam
     * @param user
     * @return
     */
    @RequestMapping("listAudioFolderBySuperId")
    @PreAuthorize("hasAnyAuthority('yinpin_quankuchaxun')")
    public Result listAudioFolderBySuperId(@RequestBody AudioFolderParam audioFolderParam, @CurrentUser Users user){
        List<AudioFolder> audioFolderList = audioService.listAudioFolderBySuperId(audioFolderParam.getSuperId());
        return Result.success(audioFolderList);
    }

    /**
     * 通过音频名称查询音频列表，支持分页
     * @param audioQueryParam
     * @param user
     * @return
     */
    @RequestMapping("queryAudio")
    @PreAuthorize("hasAnyAuthority('yinpin_quankuchaxun')")
    public Result queryAudio(@RequestBody AudioQueryParam audioQueryParam, @CurrentUser Users user){
        PageInfo<Audio> pageInfoAudio = audioService.queryAudioIncludingSubfolders(audioQueryParam, user);
        return Result.success(pageInfoAudio);
    }

    /**
     * 上传音频文件（支持多文件上传）
     * @param files
     * @param folderId
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.AUDIO_MODULE, operType = NewsConstant.ADD_OPERATION, operDesc = "上传音频到音频库")
    @RequestMapping("uploadAudio")
    @PreAuthorize("hasAnyAuthority('shangchuanyinpin')")
    public Result uploadAudio(@RequestParam(value="files") MultipartFile[] files, @RequestParam(value="audioNotes", required=false)String[] audioNotes,
                              @RequestParam(value="folderId", required=false)Integer folderId, @RequestParam(value="audioSource", required=false)String audioSource,
                              @RequestParam(value="audioEditor", required=false)String audioEditor, @CurrentUser Users user){
        for(MultipartFile multipartFile:files){
            if(multipartFile.getOriginalFilename()==null || multipartFile.getOriginalFilename().length()<=4){
                return Result.error("音频名称不能为空", "音频名称不能为空");
            }
            if(multipartFile.getOriginalFilename().length()>34){
                return Result.error("音频名称超长，不能多于30个字符", "音频名称超长，不能多于30个字符");
            }
        }
        for(String note:audioNotes){
            if(note!=null && note.length()>100){
                return Result.error("音频备注超长，不能多于100个字符", "音频备注超长，不能多于100个字符");
            }
        }
        if(audioSource!=null && audioSource.length()>64){
            return Result.error("音频来源超长，不能多于64个字符", "音频来源超长，不能多于64个字符");
        }

        if(audioEditor!=null && audioEditor.length()>16){
            return Result.error("音频编辑超长，不能多于16个字符", "音频编辑超长，不能多于16个字符");
        }
        return audioService.uploadAudio(files, audioNotes, folderId, audioSource, audioEditor, user.getUserid());
    }


    @OperLog(operModule = NewsConstant.AUDIO_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "修改音频属性")
    @RequestMapping("updateAudio")
    @PreAuthorize("hasAnyAuthority('xiugaiyinpinxinxi')")
    public Result updateAudio(@RequestBody AudioParam audioParam, @CurrentUser Users user){
        if(audioParam.getId()==null || (audioParam.getAudioName()==null && audioParam.getAudioNote()==null && audioParam.getAudioSource()==null && audioParam.getAudioEditor()==null)){
            return Result.error(ResponseEnum.PARAM_ERROR);
        }
        if(audioParam.getAudioName()!=null && audioParam.getAudioName().length()>34){
            return Result.error("音频名称超长，不能多于30个字符", "音频名称超长，不能多于30个字符");
        }
        if(audioParam.getAudioNote()!=null && audioParam.getAudioNote().length()>100){
            return Result.error("音频备注超长，不能多于100个字符", "音频备注超长，不能多于100个字符");
        }
        if(audioParam.getAudioSource()!=null && audioParam.getAudioSource().length()>64){
            return Result.error("音频备注来源超长，不能多于64个字符", "音频备注来源超长，不能多于64个字符");
        }
        if(audioParam.getAudioEditor()!=null && audioParam.getAudioEditor().length()>16){
            return Result.error("音频编辑超长，不能多于16个字符", "音频编辑超长，不能多于16个字符");
        }
        return audioService.updateAudio(audioParam, user);
    }


    /**
     * 修改音频名称
     * @param audioParam
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.AUDIO_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "修改音频名称")
    @RequestMapping("updateAudioName")
    @PreAuthorize("hasAnyAuthority('xiugaiyinpinxinxi')")
    public Result updateAudioName(@RequestBody AudioParam audioParam, @CurrentUser Users user){
        if(audioParam.getId()==null){
            return Result.error(ResponseEnum.PARAM_ERROR);
        }
        if(audioParam.getAudioName()==null || audioParam.getAudioName().length()<=4){
            return Result.error("视频名称不能为空", "视频名称不能为空");
        }
        if(audioParam.getAudioName()!=null&& audioParam.getAudioName().length()>34){
            return Result.error("音频名称超长，不能多于30个字符", "音频名称超长，不能多于30个字符");
        }
        return audioService.updateAudioName(audioParam.getId(), audioParam.getAudioName(), user.getUserid());
    }

    @OperLog(operModule = NewsConstant.AUDIO_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "修改音频备注")
    @RequestMapping("updateAudioNote")
    @PreAuthorize("hasAnyAuthority('xiugaiyinpinxinxi')")
    public Result updateAudioNote(@RequestBody AudioParam audioParam, @CurrentUser Users user){
        if(audioParam.getId()==null || audioParam.getAudioNote()==null){
            return Result.error(ResponseEnum.PARAM_ERROR);
        }
        if(audioParam.getAudioNote().length()>100){
            return Result.error("音频备注超长，不能多于100个字符", "音频备注超长，不能多于100个字符");
        }
        return audioService.updateAudioNote(audioParam.getId(), audioParam.getAudioNote(), user.getUserid());
    }


    @OperLog(operModule = NewsConstant.AUDIO_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "修改音频来源")
    @RequestMapping("updateAudioSource")
    @PreAuthorize("hasAnyAuthority('xiugaiyinpinxinxi')")
    public Result updateAudioSource(@RequestBody AudioParam audioParam, @CurrentUser Users user){
        if(audioParam.getId()==null || audioParam.getAudioSource()==null){
            return Result.error(ResponseEnum.PARAM_ERROR);
        }
        if(audioParam.getAudioSource()!=null && audioParam.getAudioSource().length()>64){
            return Result.error("音频备注来源超长，不能多于64个字符", "音频备注来源超长，不能多于64个字符");
        }
        return audioService.updateAudioSource(audioParam.getId(), audioParam.getAudioSource(), user.getUserid());
    }

    @OperLog(operModule = NewsConstant.AUDIO_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "修改音频编辑")
    @RequestMapping("updateAudioEditor")
    @PreAuthorize("hasAnyAuthority('xiugaiyinpinxinxi')")
    public Result updateAudioEditor(@RequestBody AudioParam audioParam, @CurrentUser Users user){
        if(audioParam.getId()==null || audioParam.getAudioEditor()==null){
            return Result.error(ResponseEnum.PARAM_ERROR);
        }
        if(audioParam.getAudioEditor()!=null && audioParam.getAudioEditor().length()>16){
            return Result.error("音频编辑超长，不能多于16个字符", "音频编辑超长，不能多于16个字符");
        }
        return audioService.updateAudioEditor(audioParam.getId(), audioParam.getAudioEditor(), user.getUserid());
    }

    /**
     * 删除音频文件
     * @param audioBatchParam
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.AUDIO_MODULE, operType = NewsConstant.DELETE_OPERATION, operDesc = "删除音频")
    @RequestMapping("deleteAudio2Recycle")
    @PreAuthorize("hasAnyAuthority('shanchuyinpin')")
    public Result deleteAudio2Recycle(@RequestBody AudioBatchParam audioBatchParam, @CurrentUser Users user){
        if(audioBatchParam.getIds()==null||audioBatchParam.getIds().length==0){
            return Result.error(ResponseEnum.PARAM_ERROR);
        }
        return audioService.deleteAudio2Recycle(audioBatchParam.getIds());
    }

    /**
     * 移动音频
     * @param audioBatchParam
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.AUDIO_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "移动音频")
    @RequestMapping("moveAudio")
    @PreAuthorize("hasAnyAuthority('yidongyinpin')")
    public Result moveAudio(@RequestBody AudioBatchParam audioBatchParam, @CurrentUser Users user){
        if(audioBatchParam.getIds().length==0){
            return Result.error("缺失图片id参数");
        }
        return audioService.moveAudio(audioBatchParam.getIds(), audioBatchParam.getTargetFolderId());
    }

    /**
     * 更新音频文件使用状态
     * @param
     * @param user
     * @return
     */
//    @OperLog(operModule = NewsConstant.AUDIO_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "修改音频使用状态")
//    @RequestMapping("updateAudioIsUse")
//    public Result updateAudioIsUse(@RequestBody AudioParam audioParam, @CurrentUser Users user){
//        if(user==null){
//            log.warn("用户未认证登录请求/audio/updateAudioIsUse接口");
//            return Result.error(ResponseEnum.NO_TOKEN);
//        }
//        if(audioParam.getId()==null||audioParam.getAudioIsUse()==null){
//            return Result.error(ResponseEnum.PARAM_ERROR);
//        }
//        return audioService.updateAudioIsUse(audioParam.getId(), audioParam.getAudioIsUse(), user.getUserid());
//    }

    @RequestMapping("getAudioTotalSize")
    @PreAuthorize("hasAnyAuthority('yinpin_quankuchaxun','yidongyinpin')")
    public Result getAudioTotalSize(@CurrentUser Users user){
        return audioService.getAudioTotalSize();
    }

    @RequestMapping("useAudio")
    @PreAuthorize("hasAnyAuthority('yinpin_quankuchaxun')")
    public Result useAudio(@RequestBody AudioUseParam audioUseParam, @CurrentUser Users user){
        return audioService.useAudio(audioUseParam);
    }

    @RequestMapping("updateAudioDefaultFolder")
    @PreAuthorize("hasAnyAuthority('yinpin_muluguanli')")
    public Result updateAudioDefaultFolder(@RequestBody AudioFolderParam audioFolderParam, @CurrentUser Users user){
        return audioService.updateAudioDefaultFolder(audioFolderParam);
    }

    @RequestMapping("queryAudioInRecycle")
    @PreAuthorize("hasAnyAuthority('shipin_quankuchaxun')")
    public Result queryAudioInRecycle(@RequestBody AudioRecycleQueryParam audioRecycleQueryParam, @CurrentUser Users user){
        PageInfo<Audio> pageInfoAudio = audioService.queryAudioInRecycle(audioRecycleQueryParam);
        return Result.success(pageInfoAudio);
    }

    @OperLog(operModule = NewsConstant.AUDIO_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "从回收箱恢复音频")
    @RequestMapping("restoreAudio")
    @PreAuthorize("hasAnyAuthority('yinpinkuhuishouzhanguanli')")
    public Result restoreAudio(@RequestBody AudioBatchParam audioBatchParam, @CurrentUser Users user){
        if(audioBatchParam.getIds().length==0){
            return Result.error(ResponseEnum.PARAM_ERROR);
        }
        return audioService.restoreAudio(audioBatchParam.getIds());
    }

    @OperLog(operModule = NewsConstant.AUDIO_MODULE, operType = NewsConstant.DELETE_OPERATION, operDesc = "从回收箱彻底删除音频")
    @RequestMapping("deleteAudioFromRecycle")
    @PreAuthorize("hasAnyAuthority('yinpinkuhuishouzhanguanli')")
    public Result deleteAudioFromRecycle(@RequestBody AudioBatchParam audioBatchParam, @CurrentUser Users user){
        if(audioBatchParam.getIds().length==0){
            return Result.error(ResponseEnum.PARAM_ERROR);
        }
        return audioService.deleteAudioFromRecycle(audioBatchParam.getIds());
    }

    @OperLog(operModule = NewsConstant.AUDIO_MODULE, operType = NewsConstant.DELETE_OPERATION, operDesc = "清空音频回收站")
    @RequestMapping("clearRecycle")
    @PreAuthorize("hasAnyAuthority('yinpinkuhuishouzhanguanli')")
    public Result clearRecycle(@CurrentUser Users user){
        return audioService.clearRecycle();
    }
}
